package com.colorfruit.admin.control.controller;

import com.colorfruit.admin.business.service.PurchaseService;
import com.colorfruit.framework.model.pojo.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


@Slf4j
@RestController
@RequestMapping("/purchase")
public class PurchaseController {

    @Autowired
    PurchaseService purchaseService;

    @Autowired
    HttpServletRequest request;

    /**
     * 查询采购统计列表, 如果为空显示未到达采购时间
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("/selectPurchaseCountList")
    public BaseResult selectPurchaseCountList(int page, int size) {
        log.info("# [IN] [controller] selectPurchaseCountList param: page = {}, size = {}", page, size);
        BaseResult baseResult = purchaseService.selectPurchaseCountList(page, size);
        log.info("# [OUT] [controller] selectPurchaseCountList SUCCESS");
        return baseResult;
    }

    /**
     * 查询采购统计明细列表
     * @param purchaseTotalInfoId 采购统计ID
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("/selectPurchaseDetailList")
    public BaseResult selectPurchaseDetailList(Long purchaseTotalInfoId, int page, int size) {
        log.info("# [IN] [controller] selectPurchaseDetailList param: purchaseTotalInfoId = {}, page = {}, size = {}", purchaseTotalInfoId, page, size);
        BaseResult baseResult = purchaseService.selectPurchaseDetailList(purchaseTotalInfoId, page, size);
        log.info("# [OUT] [controller] selectPurchaseDetailList SUCCESS");
        return baseResult;
    }

    /**
     * 添加采购明细
     * @param purchaseTotalInfoId 采购统计ID
     * @param supplierName 供应商名称
     * @param purchasePrice 采购价格
     * @param purchaseNumber 采购数量
     * @return
     */
    @RequestMapping("/addPurchaseDetail")
    public BaseResult addPurchaseDetail(Long purchaseTotalInfoId,
                                        String supplierName,
                                        String purchasePrice,
                                        Integer purchaseNumber) {
        log.info("# [IN] [controller] addPurchaseDetail param: purchaseTotalInfoId = {}, supplierName = {}, purchasePrice = {}, purchaseNumber = {}"
                , purchaseTotalInfoId, supplierName, purchasePrice, purchaseNumber);
        String userId = request.getHeader("userId");
        BaseResult baseResult = purchaseService.addPurchaseDetail(userId, purchaseTotalInfoId, supplierName, purchasePrice, purchaseNumber);
        log.info("# [OUT] [controller] addPurchaseDetail SUCCESS");
        return baseResult;
    }

}
